package com.bz.xtcx.manager.controller;

import com.alibaba.fastjson.JSONArray;
import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.entity.BusUserForm;
import com.bz.xtcx.manager.entity.CmsSharePlatform;
import com.bz.xtcx.manager.persist.conditions.SharePlatformCondition;
import com.bz.xtcx.manager.service.ICmsSharePlatformService;
import com.bz.xtcx.manager.utils.ExcelUtil;
import com.bz.xtcx.manager.utils.MyFileUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import com.bz.xtcx.manager.vo.VoQuery;
import com.bz.xtcx.manager.vo.VoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("shareplatform")
public class SharePlatformController {
	
	@Autowired
	private ICmsSharePlatformService cmsSharePlatformService;

    @Value("${file.tomcatPath}")
    private String tomcatPath;

	@GetMapping("page")
	public VoResponse getAllSp(@RequestParam(value = "queryStr", required = false) String queryStr, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
		VoResponse voRes = new VoResponse();
		try {
            SharePlatformCondition qc = new SharePlatformCondition();
		    if (WzStrUtil.isNotBlank(queryStr)) {
                qc.setKeyStr(queryStr);
            }
            return cmsSharePlatformService.getSps(qc, pageNum, pageSize);
		} catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("查询数据错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
	}
	
	@GetMapping("get")
	public VoResponse get(@RequestParam("id") Integer id) {
        VoResponse voRes = new VoResponse();
        if (null == id) {
            voRes.setFail(voRes);
            voRes.setMessage("参数缺失");
            return voRes;
        }
        try {
            return cmsSharePlatformService.getById(id);
        } catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("查询数据错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
	}
	
	/**
	 * 添加平台设备
	 * @param csp 平台设备信息
	 * @return 添加结果
	 */
	@PostMapping("add")
	public VoResponse add(@RequestBody CmsSharePlatform csp) {
        VoResponse voRes = new VoResponse();
        if (null == csp || WzStrUtil.isBlank(csp.getSpName())) {
            voRes.setFail(voRes);
            voRes.setMessage("参数缺少设备平台名称");
            return voRes;
        }
        try {
            return cmsSharePlatformService.add(csp);
        } catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("查询数据错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
	}
	
	@DeleteMapping(value = "del")
	public VoResponse del(@RequestParam("id") Integer id) {
        VoResponse voRes = new VoResponse();
        if (null == id) {
            voRes.setFail(voRes);
            voRes.setMessage("参数缺失");
            return voRes;
        }
        try {
            return cmsSharePlatformService.del(id);
        } catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("删除数据错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
	}

	@DeleteMapping(value = "delList")
	public VoResponse delList(@RequestParam("ids") Integer[] ids) {
        VoResponse voRes = new VoResponse();
        if (null == ids) {
            voRes.setFail(voRes);
            voRes.setMessage("参数缺失");
            return voRes;
        }
        try {
            for (Integer id : ids) {
                cmsSharePlatformService.del(id);
            }
            voRes.setMessage("成功删除平台设备");
            return voRes;
        } catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("删除数据错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
	}

	@PutMapping(value = "upd")
	public Object upd(@RequestBody CmsSharePlatform csp){
        VoResponse voRes = new VoResponse();
        if (null == csp || null == csp.getId()) {
            voRes.setFail(voRes);
            voRes.setMessage("未知设备平台");
            return voRes;
        }
        try {
            return cmsSharePlatformService.update(csp);
        } catch (XtcxException e) {
            voRes.setFail(voRes);
            voRes.setMessage("更新数据错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
	}

    /**
     * 导出中大平台
     * @param response
     * @param queryStr
     * @param token
     * @return
     * @throws IOException
     */
    @GetMapping("/exportPlatform")
    public Object exportPlatform(HttpServletResponse response, @RequestParam(value = "queryStr", required = false) String queryStr, @RequestParam("token") String token) throws IOException {
        if (WzStrUtil.isBlank(token)) {
            VoResponse voRes = new VoResponse();
            voRes.setFail(voRes);
            voRes.setMessage("未登录或登录已超时");
            return voRes;
        }
        String mimeType = "application/octet-stream";
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", "major-platform-export.xls");
        response.setHeader(headerKey, headerValue);
        response.setContentType(mimeType);
        SharePlatformCondition qc = new SharePlatformCondition();
        if (WzStrUtil.isNotBlank(queryStr)) {
            qc.setKeyStr(queryStr);
        }
        return cmsSharePlatformService.export(qc,response.getOutputStream());
    }


    /**
     * 导入中大平台
     * @return
     * @throws IOException
     */
    @PostMapping("importPlatform")
    public VoResponse importLib(@RequestParam("file") MultipartFile file) {

        MyFileUtil util = new MyFileUtil();
        String filePath = tomcatPath + "/excel-platform/";
        String [] suffixNames = {".xlsx", ".xls"};
        VoResponse voRes = util.upload(file, filePath, suffixNames);

        if(voRes.getSuccess()) {
            ExcelUtil excel = new ExcelUtil();
            String path = filePath + "/" +voRes.getData();
            List<CmsSharePlatform> cmsSharePlatforms = excel.excelTranJson(path);
            for (int i = 0; i < cmsSharePlatforms.size(); i++) {
                CmsSharePlatform cmsSharePlatform = cmsSharePlatforms.get(i);
                cmsSharePlatform.setSort((double) i);
                try {
                    cmsSharePlatformService.add(cmsSharePlatform);
                } catch (XtcxException e) {
                    e.printStackTrace();
                }
            }
            voRes.setMessage("导入成功");
            util.delFile(path);
        } else {
            return voRes;
        }
        return voRes;
    }
}
